﻿/*
' Copyright (c) 2013  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Entities.Users;
using DotNetNuke.Modules.mwOrgChart.Components;
using DotNetNuke.Services.Exceptions;

using DotNetNuke.Modules.mwOrgChart.Components.Business;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Localization;

using System.Web.UI.WebControls;

namespace DotNetNuke.Modules.mwOrgChart
{
    /// -----------------------------------------------------------------------------
    /// <summary>   
    /// The Edit class is used to manage content
    /// 
    /// Typically your edit control would be used to create new content, or edit existing content within your module.
    /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
    /// 
    /// Because the control inherits from mwOrgChartModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Node : mwOrgChartModuleBase
    {
       
        // ddl to select parent id
        private void BindNodes()
        {
            cboParentNode.Items.Clear();
            cboParentNode.DataTextField = "Name";
            cboParentNode.DataValueField = "NodeID";

            ListItem FirstItem = new ListItem();
            FirstItem.Text = Localization.GetString("SelectParentNode", this.LocalResourceFile);
            FirstItem.Value = "-1";
            cboParentNode.Items.Add(FirstItem);

            foreach (var node in mwHierarchyController.GetHierarchy())
            {
                // consider to use node.key.ToString
                cboParentNode.Items.Add(new ListItem(node.Name, node.NodeID.ToString()));
            }

            cboParentNode.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    BindNodes();
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            var node = new mwHierarchy()
            {
                NodeID = Null.NullInteger,
                Name = txtNodeName.Text,
                Shortname = txtNodeShortname.Text,
                NodeOrder = 1,
                ParentID = Convert.ToInt32(cboParentNode.SelectedValue),
                Status = HierarchyStatus.Active,
                HierarchyID = 1,

                CreatedByUserId = UserId,
                CreatedOnDate = DateTime.Now,
                LastModifiedOnDate = DateTime.Now,
                LastModifiedByUserId = UserId
            };
                                
            mwHierarchyController.AddNode(node);

            Response.Redirect(Common.Globals.NavigateURL());

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Common.Globals.NavigateURL());
        }

    }


}